package com.example.live.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.common.mvi.BaseMviFragment
import com.example.live.R
import com.example.live.adapter.RankingAll2Adapter
import com.example.live.adapter.RankingAllAdapter
import com.example.live.databinding.FragmentAllBinding
import com.example.live.entity.UserEntity
import com.example.live.intent.LiveViewIntent
import com.example.live.state.LiveUiState
import com.example.live.vm.LiveViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class AllFragment :  BaseMviFragment<FragmentAllBinding>() {
    private val vm:LiveViewModel by activityViewModels()
    private val userList= mutableListOf<UserEntity>()
    private val user2List= mutableListOf<UserEntity>()
    private lateinit var rankingAllAdapter: RankingAllAdapter
    private lateinit var rankingAll2Adapter: RankingAll2Adapter
    override fun getLayoutId(): Int =R.layout.fragment_all

    override fun initViews() {
        lifecycleScope.launch {
            vm.channel.send(LiveViewIntent.userLiveRoom)
        }
        rankingAllAdapter = RankingAllAdapter(userList)
        rankingAll2Adapter = RankingAll2Adapter(user2List)
        mDataBinding.userRv.apply {
            layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.HORIZONTAL,false)
            adapter = rankingAllAdapter
        }
        mDataBinding.userRv2.apply {
            layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
            adapter = rankingAll2Adapter
                .apply {
                    setOnClickLister(object :RankingAll2Adapter.ItemOnClickListener{
                        override fun OnClick(view: View, position: Int) {
                            super.OnClick(view, position)
                            when(view.id){
                                R.id.tv_gz ->{
                                    val tv = view.findViewById<TextView>(R.id.tv_gz)
                                    if (tv.text.equals("关注")){
                                        tv.setText("取消关注")
                                        tv.setBackgroundResource(R.drawable.login_btn_status)
                                    }else{
                                        tv.setText("关注")
                                        tv.setBackgroundResource(R.drawable.all_bg)
                                    }
                                }
                            }
                        }
                    })
                }
        }
        //切换规则
        mDataBinding.allRules.setOnClickListener {
            this@AllFragment.showToastMsg("此项目还未开发，请敬请期待！！！！")
        }

    }

    override suspend fun collectUIState() {
        super.collectUIState()
        vm.uiStateFlow.collect{
            when(it){
                is LiveUiState.userLiveRoomSuccess -> {
                    //ToastUtils.showShort(it.entity.toString())
                    userList.addAll(it.entity)
                    user2List.addAll(it.entity)
                    if (it.entity.size >=100){
                        mDataBinding.allRanking.setText("99+")
                    }else{
                        mDataBinding.allRanking.setText(it.entity.size.toString())
                    }
                    mDataBinding.allName.setText(it.entity[0].username)
                    rankingAllAdapter.notifyDataSetChanged()
                    rankingAll2Adapter.notifyDataSetChanged()
                }
                is LiveUiState.userLiveRoomFail -> this@AllFragment.showToastMsg(it.msg)
                else ->{}
            }
        }
    }

}